﻿var SubmitFormByAjax = function () {

    //-----------------------TAIKHOAN----------------------//
    var TaiKhoanSubmit = function () {
        // Submit New TaiKhoan
        $("#formCreateTaiKhoan").on('submit', function (e) {
            e.preventDefault();

            var $form = $(this);
            //if (!$form.valid()) return;
            $.ajax({
                cache: false,
                async: true,
                type: "POST",
                url: $form.attr('action'),
                data: $form.serialize(),
                success: function (data) {
                    if (data.Id == null) {
                        
                        return;
                    }

                    var trHTML = '<tr class="odd gradeX">' +
                    '<td><input type="checkbox" class="checkboxes" value="1" /></td>' +
                    '<td>' + data.Id + '</td>' +
                    '<td>' + data.Name + '</td>' +
                    '<td><a href="/TaiKhoan/Edit/' + data.Id + '">Sửa</a></td>' +
                    '<td><a href="/TaiKhoan/Delete/' + data.Id + '">Xóa</a></td></tr>';
                    $("#tableTaiKhoan").prepend(trHTML);

                    $("#close").click();
                }
            });
        });

        // Open Edit TaiKhoan
        $('.btnEditTaiKhoan').click(function () {
            var $id = $(this).attr('id');
            $.ajax({
                type: "GET",
                url: 'TaiKhoan/Edit',
                data: { id: $id },
                success: function (data) {
                    $('#bodyEditTaiKhoan').html(data);
                }
            });

        });

        // Open Delete TaiKhoan
        $('.btnDeleteTaiKhoan').click(function () {
            var $id = $(this).attr('id');
            $.ajax({
                type: "GET",
                url: 'TaiKhoan/Delete',
                data: { id: $id },
                success: function (data) {
                    $('#bodyEditTaiKhoan').html(data);
                }
            });

        });
    }

    //-----------------------DANHMUC----------------------//
    var DanhMucSubmit = function () {
        // Submit New TaiKhoan
        $("#formCreateDanhMuc").on('submit', function (e) {
            e.preventDefault();

            var $form = $(this);
            //if (!$form.valid()) return;
            $.ajax({
                cache: false,
                async: true,
                type: "POST",
                url: $form.attr('action'),
                data: $form.serialize(),
                success: function (data) {
                    if (data.Id == null) {

                        return;
                    }

                    var trHTML = '<tr class="odd gradeX">' +
                    '<td><input type="checkbox" class="checkboxes" value="1" /></td>' +
                    '<td>' + data.Name + '</td>' +
                    '<td>' + data.Type + '</td>' +
                    '<td><a href="/DanhMuc/Edit/' + data.Id + '">Sửa</a></td>' +
                    '<td><a href="/DanhMuc/Delete/' + data.Id + '">Xóa</a></td></tr>';
                    $("#tableDanhMuc").prepend(trHTML);

                    $("#close").click();
                }
            });
        });

        // Open Edit TaiKhoan
        $('.btnEditDanhMuc').click(function () {
            var $id = $(this).attr('id');
            $.ajax({
                type: "GET",
                url: 'DanhMuc/Edit',
                data: { id: $id },
                success: function (data) {
                    $('#bodyEditDanhMuc').html(data);
                }
            });

        });

        // Open Delete TaiKhoan
        $('.btnDeleteDanhMuc').click(function () {
            var $id = $(this).attr('id');
            $.ajax({
                type: "GET",
                url: 'DanhMuc/Delete',
                data: { id: $id },
                success: function (data) {
                    $('#bodyEditDanhMuc').html(data);
                }
            });

        });
    }

    //-----------------------KHACHHANG----------------------//
    var KhachHangSubmit = function () {
        // Submit New TaiKhoan
        $("#formCreateKhachHang").on('submit', function (e) {
            e.preventDefault();

            var $form = $(this);
            //if (!$form.valid()) return;
            $.ajax({
                cache: false,
                async: true,
                type: "POST",
                url: $form.attr('action'),
                data: $form.serialize(),
                success: function (data) {
                    if (data.Id == null) {

                        return;
                    }

                    var trHTML = '<tr class="odd gradeX">' +
                    '<td><input type="checkbox" class="checkboxes" value="1" /></td>' +
                    '<td>' + data.Id + '</td>' +
                    '<td>' + data.Name + '</td>' +
                     '<td>' + data.Address + '</td>' +
                    '<td>' + data.Phone + '</td>' +
                     '<td>' + data.Fax + '</td>' +
                    '<td>' + data.MST + '</td>' +
                    '<td><a href="/KhachHang/Edit/' + data.Id + '">Sửa</a></td>' +
                    '<td><a href="/KhachHang/Delete/' + data.Id + '">Xóa</a></td></tr>';
                    $("#tableKhachHang").prepend(trHTML);

                    $("#close").click();
                }
            });
        });

        // Open Edit TaiKhoan
        $('.btnEditKhachHang').click(function () {
            var $id = $(this).attr('id');
            $.ajax({
                type: "GET",
                url: 'KhachHang/Edit',
                data: { id: $id },
                success: function (data) {
                    $('#bodyEditKhachHang').html(data);
                }
            });

        });

        // Open Delete TaiKhoan
        $('.btnDeleteKhachHang').click(function () {
            var $id = $(this).attr('id');
            $.ajax({
                type: "GET",
                url: 'KhachHang/Delete',
                data: { id: $id },
                success: function (data) {
                    $('#bodyEditKhachHang').html(data);
                }
            });

        });
    }
    

    //-----------------------HANGHOA----------------------//
    var HangHoaSubmit = function () {
        // Submit New TaiKhoan
        $("#formCreateHangHoa").on('submit', function (e) {
            e.preventDefault();

            var $form = $(this);
            //if (!$form.valid()) return;
            $.ajax({
                cache: false,
                async: true,
                type: "POST",
                url: $form.attr('action'),
                data: $form.serialize(),
                success: function (data) {
                    if (data.Id == null) {

                        return;
                    }

                    var trHTML = '<tr class="odd gradeX">' +
                    '<td><input type="checkbox" class="checkboxes" value="1" /></td>' +
                    '<td>' + data.Id + '</td>' +
                    '<td>' + data.Name + '</td>' +
                    '<td><a href="/HangHoa/Edit/' + data.Id + '">Sửa</a></td>' +
                    '<td><a href="/HangHoa/Delete/' + data.Id + '">Xóa</a></td></tr>';
                    $("#tableHangHoa").prepend(trHTML);

                    $("#close").click();
                }
            });
        });

        // Open Edit TaiKhoan
        $('.btnEditHangHoa').click(function () {
            var $id = $(this).attr('id');
            $.ajax({
                type: "GET",
                url: 'HangHoa/Edit',
                data: { id: $id },
                success: function (data) {
                    $('#bodyEditHangHoa').html(data);
                }
            });

        });

        // Open Delete TaiKhoan
        $('.btnDeleteHangHoa').click(function () {
            var $id = $(this).attr('id');
            $.ajax({
                type: "GET",
                url: 'HangHoa/Delete',
                data: { id: $id },
                success: function (data) {
                    $('#bodyEditHangHoa').html(data);
                }
            });

        });
    }

    //-----------------------HANGHOA----------------------//
    var CTHangHoaSubmit = function () {
        // Submit New TaiKhoan
        $("#formCreateChiTietHangHoa").on('submit', function (e) {
            
            e.preventDefault();

            var $form = $(this);
            //if (!$form.valid()) return;
            $.ajax({
                cache: false,
                async: true,
                type: "POST",
                url: $form.attr('action'),
                data: $form.serialize(),
                success: function (data) {
                    if (data.Id == null) {
                        alert("aa");
                        return;
                    }


                    var trHTML = '<tr class="odd gradeX">' +
                    '<td><input type="checkbox" class="checkboxes" value="1" /></td>' +
                    '<td>' + data.Id + '</td>' +
                    '<td>' + data.Name + '</td>' +
                    '<td>' + data.OtherName + '</td>' +
                    '<td>' + data.SoLo + '</td>' +
                    '<td>' + data.QuyCach + '</td>' +
                    '<td>' + data.DVT + '</td>' +
                    '<td>' + data.VAT + '</td>' +
                    '<td>' + data.NgayNhap + '</td>' +
                    '<td>' + data.NgayHetHan + '</td>' +
                    '<td>' + data.DonGiaNhap + '</td>' +
                    '<td>' + data.SoLuongTon + '</td>' +
                    '<td><a href="/ChiTietHangHoa/Edit/' + data.Id + '">Sửa</a></td>' +
                    '<td><a href="/ChiTietHangHoa/Delete/' + data.Id + '">Xóa</a></td></tr>';
                    $("#tableCTHangHoa").prepend(trHTML);

                    $("#close").click();
                }
            });
        });

        // Open Edit TaiKhoan
        $('.btnEditChiTietHangHoa').click(function () {
            var $id = $(this).attr('id');
            $.ajax({
                type: "GET",
                url: 'ChiTietHangHoa/Edit',
                data: { id: $id },
                success: function (data) {
                    $('#bodyEditChiTietHangHoa').html(data);
                }
            });

        });

        // Open Delete TaiKhoan
        $('.btnDeleteChiTietHangHoa').click(function () {
            var $id = $(this).attr('id');
            $.ajax({
                type: "GET",
                url: 'ChiTietHangHoa/Delete',
                data: { id: $id },
                success: function (data) {
                    $('#bodyEditChiTietHangHoa').html(data);
                }
            });

        });
    }



    return {
        init: function () {
            TaiKhoanSubmit();
            DanhMucSubmit();
            KhachHangSubmit();
            HangHoaSubmit();
            CTHangHoaSubmit();

            // Button Close (x) in right top
            $(".close").click(function () {
                $(":input").not(':button, :submit, :reset, :hidden').val('');
            });
        }
    };
}